#include <string.h>
#include <stdio.h>
#include "mhscpu.h"

void UART_Configuration(void);

void clearBPK(void);
void setBPK(void);
void readBPK(void);
int32_t BPKTest(void);

uint32_t buf[32];
uint32_t key[32];
uint32_t clr[32];

int main(void)
{
	uint32_t i,j,k;
	
	uint32_t sysclk[] = {SYSCTRL_PLL_96MHz,SYSCTRL_PLL_72MHz};
	uint32_t hclk[] = {SYSCTRL_HCLK_Div_None,SYSCTRL_HCLK_Div2,SYSCTRL_HCLK_Div4};
	uint32_t pclk[] = {SYSCTRL_PCLK_Div2,SYSCTRL_PCLK_Div4};
	SYSCTRL_ClocksTypeDef sclock;
	
    SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU | SYSCTRL_APBPeriph_GPIO, ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU, ENABLE);

	UART_Configuration();
	
	printf("MegaHunt SCPU BPK Demo V1.0.\r\n");
	printf("UART0 Baudrate is 9600.\n");
	
	for (i = 0; i < sizeof(sysclk)/sizeof(sysclk[0]); i++)
	{
		SYSCTRL_PLLConfig((SYSCTRL_PLL_TypeDef)sysclk[i]);
		for (j = 0; j < sizeof(hclk)/sizeof(hclk[0]); j++)
		{
			SYSCTRL_HCLKConfig(hclk[j]);
			for (k = 0;k < sizeof(pclk)/sizeof(pclk[0]);k++)
			{
				SYSCTRL_PCLKConfig(pclk[k]);
				SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0, ENABLE);
				UART_Configuration();
				SYSCTRL_GetClocksFreq(&sclock);
				
				printf("\n");
				printf("sysclk is %d\n", sclock.PLL_Frequency);
				printf("hclk is %d\n", sclock.HCLK_Frequency);
				printf("pclk is %d\n", sclock.PCLK_Frequency);
				
				if(BPKTest())
					goto Failed;
				printf("Succeed\n");
			}
		}
	}
	
	printf("\n");
	printf("All Tests are Passed\n");
	printf("Test End\n");
	
	while(1)
	{
	}
	
Failed:
	printf("\n");
	printf("Test Failed\n");
}

int32_t BPKTest(void)
{
	volatile int32_t ret;
	clearBPK();
	readBPK();
	ret = memcmp(clr, key, sizeof(clr));
	if(ret)
	{
		return ret;
	}
	setBPK();
	readBPK();
	ret = memcmp(buf, key, sizeof(key));
	
	return ret;
}

void clearBPK(void)
{
	while(BPK_IsReady() == RESET);
	
	//clear buf
	memset(clr, 0, sizeof(clr));
	BPK_WriteKey(clr,sizeof(clr)/sizeof(clr[0]),0);
	printf("clrKey:\n");
}

void setBPK(void)
{
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);
	
	//set buf 0~X
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		buf[index] = index;
	}
	BPK_WriteKey(buf,sizeof(buf)/sizeof(buf[0]),0);
	printf("setKey:\n");
}

void readBPK(void)
{
	while(BPK_IsReady() == RESET);

	memset(key, 0, sizeof(key));
	BPK_ReadKey(key,sizeof(key)/sizeof(key[0]),0);
	printf("readKey:\n");
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1, GPIO_Remap_0);
	
	UART_InitStructure.UART_BaudRate = 9600;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;

	UART_Init(UART0, &UART_InitStructure);	
}

//Retarget Printf
int SER_PutChar (int ch) {

	while(!UART_IsTXEmpty(UART0));
	UART_SendData(UART0, (uint8_t) ch);
	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')  {
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






